package cn.buk.tms.entity.misc;

import java.util.Date;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.persistence.UniqueConstraint;

/**
 * 机票销售列表，来自TSL或TPR的数据
 * Created by yfdai on 2017/3/27.
 */
@Entity
@Table(name = "flt_ticket_sale", uniqueConstraints = @UniqueConstraint(name = "uk_ticket_sale", columnNames = {"enterprise_id", "ticket_no", "ticket_status"}))
public class TicketSale {

    /**
     * 未知状态
     */
    public static final  int TICKET_STATUS_NONE = -1;
    /**
     * 出票
     */
    public static final  int TICKET_STATUS_PRINTED = 0;
    /**
     * 改签
     */
    public static final  int TICKET_STATUS_EX = 1;
    /**
     * 退票
     */
    public static final  int TICKET_STATUS_REFUND = 4;
    /**
     * 废票
     */
    public static final  int TICKET_STATUS_VOID = 16;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "enterprise_id")
    private int enterpriseId;

    @Column(length = 14, name = "ticket_no")
    private String ticketNo;

    /**
     * 出票日期
     */
    @Temporal(TemporalType.DATE)
    @Column(name = "sell_date")
    private Date sellDate;

    /**
     * 机票的国际国内属性
     * -1 - 未知
     * 0 - 国内
     * 1 - 国际
     */
    private Integer type3;

    @Column(length = 3, name = "bal_code")
    private String balCode;

    @Column(length = 3)
    private String orig;

    @Column(length = 3)
    private String dest;

    @Column(name = "pnr", length = 6)
    private String pnrNo;

    /**
     * 出票office
     */
    @Column(name = "office_no", length = 6)
    private String officeNo;

    @Column(name = "device_id")
    private int deviceId;

    /**
     * 如果是出票，此处为航司销售价
     * 如果是改签票，此处为空或价格
     * 如果是退票，此处为航司实退
     */
    private Double price;

    /**
     * 如果是出票，此处为税收
     * 如果是改签票，则此处是航司改签费
     */
    private Double tax;

    /**
     * 如果是出票，此处为航段奖励
     * 如果是改签票，则此处是0.00%
     */
    private Double commission;

    /**
     * 底价, 采购价
     */
    @Column(name = "bottom_price")
    private Double bottomPrice;

    /**
     * 支付备注
     */
    @Column(name = "pay_memo", length = 30)
    private String payMemo;

    /**
     * 采购点，出票处, 供应商
     * 0, null - BSP
     */
    @ManyToOne
    @JoinColumn(name="supplier_id")
    private Supplier supplier;

    @Column(name = "ctcm_count")
    private int ctcmCount;

    @Column(length = 80)
    private String detail;

    /**
     * 操作员: 录入或导入该票号的当前用户的用户名
     */
    @Column(length = 32)
    private String operator;

    /**
     * 检验这条票号是否在差旅系统中，并已销账
     * null/0 - 默认值（原始状态）
     * 2 - 在系统中已找到（已匹配，账单审核通过）
     * 1 - 在系统中已找到（未完全匹配）（保留）
     * 4 - 在系统中未找到（保留）
     */
    @Column(name = "check_status")
    private Integer checkStatus;

    /**
     * 账单号, 用于记录自动审核时匹配的账单号
     */
    @Column(length = 32, name = "bill_no")
    private String billNum;




    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time", insertable = false, updatable = false)
    private Date createTime;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(insertable = false)
    private Date lastupdate;

    /**
     * 0 - 未将pnrNo保存到实体Pnr(t240)中
     * 1 - 保存过
     */
    private Integer status;



    /**
     * 客票的状态
     * -1 - 以前的数据，当时未记录状态
     * 0 - 出票
     * 1 - 改签
     * 4 - 退票
     * 16 - 废票
     */
    @Column(name = "ticket_status")
    private Integer ticketStatus;

    @Column(name = "conj_ticket", length = 5)
    private String conjTag;


    // 以下信息来自Pnr(t240)
    @Column(name = "eterm_username", length = 32)
    private String etermUsername;

    @Column(name = "link_phone")
    private String linkPhone;

    @Column(name = "seg_status")
    private String segStatus; // 航段状态

    @Column(name = "seg_count")
    private Integer segCount;

    @Column(name = "psg_count")
    private Integer psgCount;

    @Column(name = "pnr_create_time")
    private Date pnrCreateTime;

    @Column(name = "sd_office", length = 6)
    private String bookingOffice; // 预定office

    //   以下信息来自DetrInfo
    @Column(length=50, name = "psg_name")
    private String psgName;

    @Column(length=30, name = "seg_info")
    private String segInfo;

    @Column(length=2)
    private String carrier;

    @Column(length=32, name = "flt_no")
    private String flightNo;

    @Column(length=7)
    private String subclass;

    @Column(length=45, name = "flt_date")
    private String fltDate;

    @Column(name = "tax_cn")
    private Double taxCn;

    @Column(name = "tax_yq")
    private Double taxYq;

    @Column(name = "tax_ot")
    private Double taxOt;


    private Double fare;

    @Column(name = "total_fare")
    private Double totalFare;

    @Column(length=15)
    private String luggage;

    @Column(columnDefinition="char(1)", name = "is_used")
    private String isUsed;

    @Column(length=2000, name = "detr_detail")
    private String detrDetail;


    // 以下记录从rtkt中获取的内容

    @Column(length=2000, name = "rtkt_detail")
    private String rtktDetail;

    /**
     * 票样中的fc项
     */
    private String fc;

    /**
     * 始发国货币
     */
    @Column(name = "from_currency")
    private String fromCurrency;

    /**
     * 始发国票价（不含税)
     */
    @Column(name = "from_fare")
    private Double fromFare;

    /**
     * 本地货币
     */
    @Column(name = "local_currency")
    private String localCurrency;

    /**
     * 本地货币票价（不含税，人民币）
     */
    @Column(name = "local_currency_fare")
    private Double localFare;

    /**
     * NUC计算的票价（不含税）
     */
    @Column(name = "nuc_fare")
    private Double nucFare;

    private Double roe;

    /**
     * 记录RTKT中total fare栏中显示的BT等状态
     */
    @Column(name = "total_fare_none",length = 10)
    private String totalFareNone;

    @Column(name = "total_fare_currency",length = 3)
    private String totalFareCurrency;

    @Column(name = "total_tax")
    private Double totalTax;

    /**
     * 净价
     */
    @Column(name = "net_fare")
    private Double netFare;

//    @OneToMany(mappedBy = "ticketInfo", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
//    private List<TicketFlightInfo> flights;
//
//    @OneToMany(mappedBy = "ticketInfo", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
//    private List<TicketFareInfo> fares;


    public String getUseStatus() {
        if ("0".equalsIgnoreCase(this.isUsed)) {
          return "未使用";
        } else if ("2".equalsIgnoreCase(this.isUsed)) {
          return "已使用";
        } else {
          return "";
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public String getTicketNo() {
        return ticketNo;
    }

    public void setTicketNo(String ticketNo) {
        if (ticketNo != null) {
            ticketNo = ticketNo.trim();
            if (ticketNo.length() == 13) {
                this.balCode = ticketNo.substring(0, 3);
                ticketNo = this.balCode + "-" + ticketNo.substring(3);
            } else if (ticketNo.length() == 14) {
                this.balCode = ticketNo.substring(0, 3);
            } else {
                return;
            }
        }

        this.ticketNo = ticketNo;
    }

    public Date getSellDate() {
        return sellDate;
    }

    public void setSellDate(Date sellDate) {
        this.sellDate = sellDate;
    }

    public String getBalCode() {
        return balCode;
    }

    public void setBalCode(String balCode) {
        this.balCode = balCode;
    }

    public String getOrig() {
        return orig;
    }

    public void setOrig(String orig) {
        this.orig = orig;
    }

    public String getPnrNo() {
        return pnrNo;
    }

    public void setPnrNo(String pnrNo) {
        this.pnrNo = pnrNo;
    }

    public String getOfficeNo() {
        return officeNo;
    }

    public void setOfficeNo(String officeNo) {
        this.officeNo = officeNo;
    }

    public int getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(int deviceId) {
        this.deviceId = deviceId;
    }

    public int getCtcmCount() {
        return ctcmCount;
    }

    public void setCtcmCount(int ctcmCount) {
        this.ctcmCount = ctcmCount;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastupdate() {
        return lastupdate;
    }

    public void setLastupdate(Date lastupdate) {
        this.lastupdate = lastupdate;
    }

    public String getDest() {
        return dest;
    }

    public void setDest(String dest) {
        this.dest = dest;
    }

    public Double getTaxCn() {
        return taxCn == null ? 0 : taxCn;
    }

    public void setTaxCn(Double taxCn) {
        this.taxCn = taxCn;
    }

    public Double getTaxYq() {
        return taxYq == null ? 0 : taxYq;
    }

    public void setTaxYq(Double taxYq) {
        this.taxYq = taxYq;
    }

    public Double getTaxOt() {
        return taxOt == null ? 0 : taxOt;
    }

    public void setTaxOt(Double taxOt) {
        this.taxOt = taxOt;
    }

    public double getTax() {
        return tax == null ? 0 : tax;
    }

    public void setTax(Double tax) {
        this.tax = tax;
    }

    public Double getFare() {
        return fare == null ? 0 : fare;
    }

    public void setFare(Double fare) {
        this.fare = fare;
    }

    public Double getTotalFare() {
        return totalFare == null ? 0 : totalFare;
    }

    public void setTotalFare(Double totalFare) {
        this.totalFare = totalFare;
    }

    public String getPsgName() {
        return psgName;
    }

    public void setPsgName(String psgName) {
        this.psgName = psgName;
    }

    public String getSegInfo() {
        return segInfo;
    }

    public void setSegInfo(String segInfo) {
        this.segInfo = segInfo;
    }

    public String getCarrier() {
        return carrier;
    }

    public void setCarrier(String carrier) {
        this.carrier = carrier;
    }

    public String getFlightNo() {
        return flightNo;
    }

    public void setFlightNo(String flightNo) {
        this.flightNo = flightNo;
    }

    public String getSubclass() {
        return subclass;
    }

    public void setSubclass(String subclass) {
        this.subclass = subclass;
    }

    public String getFltDate() {
        return fltDate;
    }

    public void setFltDate(String fltDate) {
        this.fltDate = fltDate;
    }

    public String getLuggage() {
        return luggage;
    }

    public void setLuggage(String luggage) {
        this.luggage = luggage;
    }

    public String getIsUsed() {
        return isUsed;
    }

    public void setIsUsed(String isUsed) {
        this.isUsed = isUsed;
    }

    public String getDetrDetail() {
        return detrDetail;
    }

    public void setDetrDetail(String detrDetail) {
        this.detrDetail = detrDetail;
    }

    public String getEtermUsername() {
        return etermUsername == null ? "" : etermUsername.trim();
    }

    public void setEtermUsername(String etermUsername) {
        this.etermUsername = etermUsername;
    }

    public String getLinkPhone() {
        return linkPhone;
    }

    public void setLinkPhone(String linkPhone) {
        this.linkPhone = linkPhone;
    }

    public String getSegStatus() {
        return segStatus;
    }

    public void setSegStatus(String segStatus) {
        this.segStatus = segStatus;
    }

    public Integer getSegCount() {
        return segCount;
    }

    public void setSegCount(Integer segCount) {
        this.segCount = segCount;
    }

    public Date getPnrCreateTime() {
        return pnrCreateTime;
    }

    public void setPnrCreateTime(Date pnrCreateTime) {
        this.pnrCreateTime = pnrCreateTime;
    }

    public String getBookingOffice() {
        return bookingOffice;
    }

    public void setBookingOffice(String bookingOffice) {
        this.bookingOffice = bookingOffice;
    }

    public void setPsgCount(Integer psgCount) {
        this.psgCount = psgCount;
    }

    public Integer getStatus() {
        return status == null ? 0 : status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getRtktDetail() {
        return rtktDetail;
    }

    public void setRtktDetail(String rtktDetail) {
        this.rtktDetail = rtktDetail;
    }

    public String getFc() {
        return fc;
    }

    public void setFc(String fc) {
        this.fc = fc;
    }

    public Double getRoe() {
        return roe;
    }

    public void setRoe(Double roe) {
        this.roe = roe;
    }

    public String getFromCurrency() {
        return fromCurrency;
    }

    public void setFromCurrency(String fromCurrency) {
        this.fromCurrency = fromCurrency;
    }

    public Double getFromFare() {
        return fromFare;
    }

    public void setFromFare(Double fromFare) {
        this.fromFare = fromFare;
    }

    public String getLocalCurrency() {
        return localCurrency;
    }

    public void setLocalCurrency(String localCurrency) {
        this.localCurrency = localCurrency;
    }

    public Double getLocalFare() {
        return localFare;
    }

    public void setLocalFare(Double localFare) {
        this.localFare = localFare;
    }

    public Double getNucFare() {
        return nucFare;
    }

    public void setNucFare(Double nucFare) {
        this.nucFare = nucFare;
    }

    public String getTotalFareNone() {
        return totalFareNone;
    }

    public void setTotalFareNone(String totalFareNone) {
        this.totalFareNone = totalFareNone;
    }

    public String getTotalFareCurrency() {
        return totalFareCurrency;
    }

    public void setTotalFareCurrency(String totalFareCurrency) {
        this.totalFareCurrency = totalFareCurrency;
    }

//    public List<TicketFlightInfo> getFlights() {
//        if (flights == null) flights = new ArrayList<>();
//        return flights;
//    }
//
//    public void setFlights(List<TicketFlightInfo> flights) {
//        this.flights = flights;
//    }
//
//    public List<TicketFareInfo> getFares() {
//        if (fares == null) fares = new ArrayList<>();
//        return fares;
//    }
//
//    public void setFares(List<TicketFareInfo> fares) {
//        this.fares = fares;
//    }
//
//    public Double getTotalTax() {
//        return totalTax;
//    }

    public void setTotalTax(Double totalTax) {
        this.totalTax = totalTax;
    }

    public Double getNetFare() {
        return netFare;
    }

    public void setNetFare(Double netFare) {
        this.netFare = netFare;
    }

    public int getType3() {
        return type3 == null ? -1 : type3;
    }

    public void setType3(Integer type3) {
        this.type3 = type3;
    }

    public double getPrice() {
        return price == null ? 0 : price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public double getCommission() {
        return commission == null ? 0 : commission;
    }

    public void setCommission(Double commission) {
        this.commission = commission;
    }

    public Double getBottomPrice() {
        if (bottomPrice == null) {
            bottomPrice = this.getPrice() + this.getTax() - this.getCommission();
        }
        return bottomPrice;
    }

    public void setBottomPrice(Double bottomPrice) {
        this.bottomPrice = bottomPrice;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public String getPayMemo() {
        return payMemo;
    }

    public void setPayMemo(String payMemo) {
        this.payMemo = payMemo;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public Integer getCheckStatus() {
        return checkStatus == null ? 0 : checkStatus;
    }

    public void setCheckStatus(Integer checkStatus) {
        this.checkStatus = checkStatus;
    }

    public String getBillNum() {
        return billNum;
    }

    public void setBillNum(String billNum) {
        this.billNum = billNum;
    }

    public Double getTotalTax() {
        return totalTax;
    }

    public int getTicketStatus() {
        return ticketStatus == null ? -1 : ticketStatus;
    }

    public void setTicketStatus(Integer ticketStatus) {
        this.ticketStatus = ticketStatus;
    }

    /**
     * 联程客票的标志
     */
    public String getConjTag() {
        return conjTag;
    }

    public void setConjTag(String conjTag) {
        this.conjTag = conjTag;
    }
}
